<script>
	import customeApi from '@/service/custome.js';
	import LivePlayer from '@/service/player.js';
	import service_player from '@/service/player.js';
	import service_enter from '@/service/login.js'
	// var gioConfig = require("@/components/GrowingIO/gioConfig.js").default;
	export default {
		globalData: {
			single_id: '',
			product_id: '',
			gid: '',
			storeId: '',
			share_uid: '',
			isIphone: false,
			programMobile: '',
			programuser_id: '',
			//小程序直播微信登录
			openid:'',
			nickName:'',
			avatarUrl:'',
			userMobile:'',
		},
		onLaunch(options) {
			const that = this;
			// gio('getLocation')
			var openid = uni.getStorageSync('nowOpenid');
			var unionid = uni.getStorageSync('unionid');
			var userInfo = uni.getStorageSync('userInfo');
			if (openid) {
				// console.log(gioConfig);
				// #ifdef MP-WEIXIN
				gio('identify', openid, unionid);
				gio('setUserId', openid);
				gio('setUser', {
					id: openid,
					name: userInfo.nickName
				});
				gio('userID', openid);
				// #endif
			}
			uni.getSystemInfo({
				success: function(res) {
					if (res.safeArea.top > 20) {
						// console.log(res);
						that.globalData.isIphone = true;
					}
				}
			})
			this.getlng()
		},
		onShow(options) {
			// if (!this.$store.state.tim.status && uni.getStorageSync('timInfo')) {
			// 	this.getIM();
			// }
			// console.log(options,'options=====')
			if (options.referrerInfo) {
				if (options.referrerInfo.extraData) {
					if (options.referrerInfo.extraData.share_uid) {
						this.globalData.share_uid = options.referrerInfo.extraData.share_uid;
					}
					if (options.referrerInfo.extraData.gid) {
						this.globalData.gid = options.referrerInfo.extraData.gid;
					}
					if (options.referrerInfo.extraData.storeId) {
						this.globalData.storeId = options.referrerInfo.extraData.storeId;
					}
					if (options.referrerInfo.extraData.product_id) {
						this.globalData.product_id = options.referrerInfo.extraData.product_id;
					}
					if (options.referrerInfo.extraData.single_id) {
						this.globalData.single_id = options.referrerInfo.extraData.single_id;
					}
					if (options.referrerInfo.extraData.mobile) {
						this.globalData.programMobile = options.referrerInfo.extraData.mobile
					}
					if (options.referrerInfo.extraData.user_id) {
						this.globalData.programuser_id = options.referrerInfo.extraData.user_id
					}
				}
			}
			// #ifdef  MP-WEIXIN
			let livePlayers = requirePlugin('live-player-plugin');
			// #endif
			const sceneList = [1007, 1008, 1014, 1044, 1045, 1046, 1047, 1048, 1049, 1073, 1154, 1155]
			
			if (sceneList.includes(options.scene)) {
				var _this = this;
				livePlayers.getShareParams()
					.then(res => {
						// 房间号 res.room_id // 用户openid res.openid //分享者openid res.share_openid// 页面路径上携带的自定义参数 res.custom_params
						let custom_params = res.custom_params; //分享者
						let room_id = res.room_id; //房间号
						if (custom_params == '' || custom_params == null) {
							uni.navigateTo({
								url: '/pages/livePlayer/index/index?id=' + room_id
							});
						} else {
							let userIds = uni.getStorageSync('userId')
							_this.getxcxLiveShare(custom_params,room_id,userIds);
						}
					}).catch(err => {
						console.log('get share params', err)
					})
			}
			this.getLvieConfig();
		},
		onHide() {
			// console.log('App Hide')
		},
		computed: {
			timMsg() {
				return this.$store.state.tim.msg;
			},
			token() {
				return uni.getStorageSync('token');
			}
		},
		methods: {
			getlng() {
				let that = this;
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						uni.setStorageSync('location_longitude', res.longitude);
						uni.setStorageSync('location_latitude', res.latitude);
					}
				})
			},
			getIM() {
				customeApi.getIM().then(res => {
					if (res.code === 1) {
						uni.setStorageSync('timInfo', {
							userID: res.data.im_user,
							userSig: res.data.sig
						});
						this.$tim.login(res.data.im_user, res.data.sig);
					} else {
						this.$utils.showErr({
							title: res.msg
						});
					}
				});
			},
			//直播配置开关
			getLvieConfig() {
				LivePlayer.getLvieConfig()
					.then(res => {
						if (res.code === 1) {
							// console.log(res.data);
							let text = res.data.value == 0 ? '话题' : res.data.title;
							console.log(text);
							// uni.setTabBarItem({
							// 	index: 2,
							// 	text: text
							// });
							uni.setStorageSync('LiveConfig', text);
						} else {}
					})
					.catch(err => {
						console.log(err);
					});
			},
			//小程序直播分享
			getxcxLiveShare(e,room_id,userIds) {
				if (!userIds || userIds=='' || userIds==null || userIds<0 || userIds==undefined) {
					uni.setStorageSync('liveShare', e); //分享用户
					uni.setStorageSync('Router', 'pages/livePlayer/index/index?id=' + room_id)
					var _this=this;
					uni.showModal({
					    title: '提示',
					    content: '请先登录',
					    success: function (res) {
					        if (res.confirm) {
					          _this.wxUserLogin(e,room_id);
					        } else if (res.cancel) {
								_this.wxUserLogin(e,room_id);
					            // uni.navigateTo({
					            // 	url: '/pages_login/pages/login/login/index'
					            // });
					        }
					    }
					});
				} else {
					let user_id = e;
					let params = {
						invit_user_id: user_id,
						roomid: room_id
					};
					let roomId = [room_id]; // 房间号
					service_player.xcxLiveShare(params).then(res => {
						if (res) {
							uni.navigateTo({
								url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${userIds}`
							});
						}
					}).catch(err => {

					});
				}
			},
			//小程序直播微信登录
			/**
			 * @param {Object} userId 邀请用户
			 * @param {Object} room_id 直播房间
			 */
			wxUserLogin(userId,room_id) {
				let that = this
				let single_id = uni.getStorageSync('single_id')
				let root = uni.getStorageSync('root') == "true" ? true : false
				let product_id = uni.getStorageSync('id')
				let gid = uni.getStorageSync('gid')
				let u = uni.getStorageSync('u')
				let NewName = uni.getStorageSync('NewName')
				uni.getUserProfile({
					desc: '用于完善会员资料',
					lang: 'zh_CN',
					success: e => {
						uni.setStorageSync('userInfo', e.userInfo)
						// 统计获取用户信息
						gio('setVisitor', e.userInfo);
						that.globalData.nickName = e.userInfo.nickName
						that.globalData.avatarUrl = e.userInfo.avatarUrl
						that.$utils.login({
							success: (res) => {
								// console.log(res);
								service_enter.getToken({
										code: res.code,
										nickName: that.globalData.nickName,
										avatarUrl: that.globalData.avatarUrl,
										sence: '',
										wx_access_token: '',
										access_token:'',
										unionid: '',
										openid: '',
									})
									.then(res => {
										uni.hideLoading()
										if (res.code == 1) {
											let data = res.data
											that.globalData.openid = res.data.openid;
											if (!data.mobile || data.mobile == '') {
												that.globalData.mobileshow = true
											} else {
												let userMobile = res.data.mobile
												that.globalData.userMobile = userMobile
												if(data.user_id!='' || data.user_id!=null){
													uni.setStorageSync('userId', data.user_id);
													that.getxcxLiveShare(userId,room_id,data.user_id)
												}
												console.log('token', data.token.access_token)
												uni.setStorageSync('token', data.token.access_token);
											}
										} else {
											that.$utils.hideLoading()
											that.$utils.showErr({
												title: res.msg
											})
										}
									})
							}
						})
					}
				})
			},
		},
		watch: {
			timMsg(newVal, oldVal) {
				console.log(newVal);
			},
			token(newVal, oldVal) {
				if (newVal) {
					this.getIM();
				} else {
					this.$tim.logout();
				}
			}
		}
	};
</script>

<style lang="scss">
	// uView
	@import '@/plugins/uview-ui/index.scss';
	// 字体图标
	@import '@/icons/iconfont.css';

	// 重置样式
	page {
		color: #333;
		font-size: 28rpx;
		background: #f6f7fb;
	}

	image {
		width: 100%;
		height: 100%;
		display: block;
	}

	input {
		min-height: 0;
		flex: 1;
	}

	view,
	scroll-view,
	swiper,
	swiper-item,
	cover-view,
	cover-image,
	icon,
	text,
	rich-text,
	progress,
	button,
	checkbox,
	form,
	input,
	label,
	radio,
	slider,
	switch,
	textarea,
	navigator,
	audio,
	camera,
	image,
	video {
		box-sizing: border-box;
	}

	button::after {
		border: none;
	}

	swiper {
		height: 100%;
	}

	/* #ifdef MP-WEIXIN || APP-PLUS */
	::-webkit-scrollbar {
		display: none;
	}

	/* #endif */

	// 公共样式
	/* input 样式 */
	.placeholder {
		color: #939393;
	}

	/* 超出多行省略 */
	.omit-1 {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.omit-2 {
		display: -webkit-box;
		overflow: hidden;
		white-space: normal !important;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.omit-3 {
		display: -webkit-box;
		overflow: hidden;
		white-space: normal !important;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
	}

	.omit-4 {
		display: -webkit-box;
		overflow: hidden;
		white-space: normal !important;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 4;
		-webkit-box-orient: vertical;
	}

	// 隐藏滚动条
	.hide-scroll ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}

	.of-hidden {
		overflow: hidden;
	}

	.pR-32 {
		padding-right: 32rpx !important;
	}

	.mR-4 {
		margin-right: 4rpx !important;
	}

	.col-fff {
		color: #fff !important;
	}

	.col-666 {
		color: #666 !important;
	}

	.col-c6 {
		color: #c6c6c6 !important;
	}

	.col-ccc {
		color: #ccc !important;
	}

	.fs-16 {
		font-size: 16rpx !important;
	}

	.fs-20 {
		font-size: 20rpx !important;
	}

	.fs-24 {
		font-size: 24rpx !important;
	}

	.fs-28 {
		font-size: 28rpx !important;
	}

	.ft-normal {
		font-weight: normal !important;
	}

	.ft-bold {
		font-weight: bold !important;
	}

	// 修改uView样式
	.u-loading {
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
